#!/bin/bash

# Color
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

managers=($(ls -F | grep "/$"))

for ((i=1;i<=${#managers[@]};i++ )); do
        selectd="${managers[$i-1]}"
        echo -e "${green}${i}${plain}) ${selectd}"
    done

read -p "请选择管理器(默认: ${managers[0]}):" pick
    [ -z "$pick" ] && pick=1
    expr ${pick} + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "[${red}错误${plain}] 无效的管理器索引"
        continue
    fi
    if [[ "$pick" -lt 1 || "$pick" -gt ${#managers[@]} ]]; then
        echo -e "[${red}错误${plain}] 管理器索引介于 1 到 ${#managers[@]}"
        continue
    fi
    manager=${managers[$pick-1]}
    echo
    echo "---------------------------"
    echo "当前选择的管理器 = ${manager}"
    echo "---------------------------"
    echo



execute(){
scripts=($(ls $1))
for ((i=1;i<=${#scripts[@]};i++ )); do
        selectd="${scripts[$i-1]}"
        echo -e "${green}${i}${plain}) ${selectd}"
    done

read -p "请选择脚本(默认: ${scripts[0]}):" pick
    [ -z "$pick" ] && pick=1
    expr ${pick} + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "[${red}错误${plain}] 无效的脚本索引"
        continue
    fi
    if [[ "$pick" -lt 1 || "$pick" -gt ${#scripts[@]} ]]; then
        echo -e "[${red}错误${plain}] 脚本索引介于 1 到 ${#scripts[@]}"
        continue
    fi
    script=${scripts[$pick-1]}
    echo
    echo "---------------------------"
    echo "当前选择的脚本 = ${script}"
    echo "---------------------------"
    echo
cd $1
sed -i 's/\r$//' $script #解决如果在windows中编辑过该脚本文件在linux执行时会报坏的解释器的问题
chmod +x $script
./$script

}

execute $manager

